package com.iotechn.unimall.admin.myapi.course;

import com.baomidou.mybatisplus.extension.api.ApiController;
import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.mapper.mymapper.ICourseMapper;
import com.iotechn.unimall.mydata.domain.CourseBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@Service
public class ICourseServiceImpl implements ICourseService {

    @Autowired
    private ICourseMapper iCourseMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public String createParent(String title) throws ServiceException {
        CourseBean courseBean = new CourseBean();
        courseBean.setTitle(title);
        courseBean.setParentid(0L);
        courseBean.setPath(null);

        iCourseMapper.insert(courseBean);

        return "ok";
    }

//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public String createSon(String title, Long parentId, MultipartFile upload) throws ServiceException, IOException {
//
//        String path = ApiController.class.getResource("../../../../../uploads").getFile();
//        File file = new File(path);
//
//        if(!file.exists()) {
//            file.mkdirs();
//        }
//
//        if (upload==null){
//            throw new AdminServiceException(ExceptionDefinition.PARAM_DOCUMENT_NO_EXIST);
//        }
//
//        // 获取到上传文件的名称
//        String filename = upload.getOriginalFilename();
//
//        String uuid = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
//        // 把文件的名称唯一化
//        filename = uuid+"_"+filename;
//        // 上传文件
//        upload.transferTo(new File(file,filename));
//
//        CourseBean courseBean = new CourseBean();
//        courseBean.setPath(filename);
//        courseBean.setTitle(title);
//        courseBean.setParentid(parentId);
//        iCourseMapper.insert(courseBean);
//
//        return "ok";
//    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseEntity<byte[]> download(String filename) throws ServiceException, IOException {

        //String path = ApiController.class.getResource("E:\\unimall\\unimall\\unimall\\unimall-launcher\\src\\main\\resources\\uploads").getFile();
        String path = "E:\\unimall\\unimall\\unimall\\unimall-launcher\\src\\main\\resources\\uploads";
        //3. 通过io流的方式 把 要下载的文件 读入到一个 字节数组中 (相当于一个缓存)
        File f = new File(path,filename);
        InputStream is = new FileInputStream(f);
        byte[] body = new byte[is.available()];
        is.read(body);//把要下载的文件 都 读到 body 字节数组中了  字节适用于所有的文件

        //4.得到这个body缓存 返回浏览器
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Disposition", "attchement;filename=" + URLEncoder.encode(f.getName(), "UTF-8"));
        HttpStatus statusCode = HttpStatus.OK;
        ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(body, headers, statusCode);

        is.close();

        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String update(Long courseId,String title, Long parentId) throws ServiceException {

        CourseBean courseBean = iCourseMapper.selectById(courseId);
        courseBean.setTitle(title);
        courseBean.setParentid(parentId);

        iCourseMapper.updateById(courseBean);
        return "ok";
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String delete(String courseId) throws ServiceException {
        iCourseMapper.deleteById(courseId);
        return "ok";
    }

    @Override
    public List<String> returnParentName() throws ServiceException {
        HashMap<String, Object> map = new HashMap<>();
        map.put("parentId",0);
        List<CourseBean> courseBeans = iCourseMapper.selectByMap(map);
        ArrayList<String> strings = new ArrayList<>();
        for (CourseBean courseBean : courseBeans) {
            strings.add(courseBean.getTitle());
        }
        return strings;
    }

    @Override
    public List<CourseBean> returnParent() throws ServiceException {
        HashMap<String, Object> map = new HashMap<>();
        map.put("parentId",0);
        List<CourseBean> courseBeans = iCourseMapper.selectByMap(map);

        return courseBeans;
    }

    @Override
    public List<CourseBean> getSon(Long parentId) throws ServiceException {
        HashMap<String, Object> map = new HashMap<>();
        map.put("parentId",parentId);
        List<CourseBean> courseBeans = iCourseMapper.selectByMap(map);
        return courseBeans;
    }


}
